package com.api.gbhg.liubing.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liubing.dao.EducationTrainingDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : liubing
 * @date :  2020/12/30 15:55
 * 教育培训经费
 */
public class EducationTrainingAction {
    public static final Logger LOGGER= LoggerFactory.getLogger(PropertyManagementAction.class);
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    // 教育培训经费
    @POST
    @Path("/getEducationTrainingAction")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getEducationTrainingAction(Map<String,String> param) {
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
        String requestId=param.get("requestId");
        EducationTrainingDao dao = new EducationTrainingDao();
//        RecordSetTrans mainTable = dao.getMainTable(status,pageNum,pageSize);//获取主表数据
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
//        int total = dao.getMainTableCount(status);//获取数据总条数
//        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);
        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()){
            map[i]=new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }

            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr",loginid);  //编报人
            BigDecimal hwgz=new BigDecimal("0.00");
            BigDecimal gc=new BigDecimal("0.00");
            BigDecimal fw=new BigDecimal("0.00");
            BigDecimal xecg=new BigDecimal("0.00");
            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("yszje",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            BigDecimal zxys = new BigDecimal("0.00"); //专项预算
            BigDecimal fzxys = new BigDecimal("0.00");//非专项预算
            BigDecimal je = new BigDecimal("0.00");//金额

          //金额
            while (rs.next()){
                if (rs.getString("sfzxys").equals("0")){
                    zxys=zxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }else if (rs.getString("sfzxys").equals("1")){
                    fzxys=fzxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }
                //je=je+rs.getInt("je");
            }
            //int nys=zxys+fzxys;
            map[i].put("zxys",zxys.toString());//小额采购
            map[i].put("fzxys",fzxys.toString());//非专项预算
            map[i].put("je",je);//金额
            map[i].put("nys",mainTable.getString("yszje"));//年预算
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_jypxjf");
        return  JSON.toJSONString(page);
    }
//教育培训经费
    public String getEducationTrainingAction1(String requestId) {
//        String status =  param.get("status");//状态
//        Integer pageNum= Integer.valueOf(param.get("pageNum"));//页数
//        Integer pageSize= Integer.valueOf(param.get("pageSize"));//显示数量
//        String requestId=param.get("requestId");
        EducationTrainingDao dao = new EducationTrainingDao();
//        RecordSetTrans mainTable = dao.getMainTable(status,pageNum,pageSize);//获取主表数据
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
//        page.setPageNumber(pageNum);
//        page.setpageSize(pageSize);
//        int total = dao.getMainTableCount(status);//获取数据总条数
//        page.setTotal(total);//数据总条数
//        page.setPages((total + pageSize - 1) / pageSize);
        Map<String,Object>[] map=new HashMap[mainTable.getArray().size()];
        int i=0;
        while (mainTable.next()){
            map[i]=new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }

            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr",loginid);  //编报人
            BigDecimal wan = new BigDecimal("10000");
            BigDecimal hwgz=new BigDecimal("0.0000");
            BigDecimal gc=new BigDecimal("0.0000");
            BigDecimal fw=new BigDecimal("0.0000");
            BigDecimal xecg=new BigDecimal("0.0000");
            if (!mainTable.getString("hwgz").equals("")){
                hwgz=hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("gc").equals("")){
                gc=gc.add(new BigDecimal(mainTable.getDouble("gc")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("fw").equals("")){
                fw=fw.add(new BigDecimal(mainTable.getDouble("fw")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            if (!mainTable.getString("xecg").equals("")){
                xecg=xecg.add(new BigDecimal(mainTable.getDouble("xecg")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
            }
            map[i].put("yszje",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            RecordSet rs = dao.getDetailedTable(mainTable.getString("requestid"), 1);
            BigDecimal zxys = new BigDecimal("0.0000"); //专项预算
            BigDecimal fzxys = new BigDecimal("0.0000");//非专项预算
            BigDecimal je = new BigDecimal("0.0000");//金额

            //金额
            while (rs.next()){
                if (rs.getString("sfzxys").equals("1")){
                    zxys=zxys.add(new BigDecimal(rs.getDouble("jfysy")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
                }else if (rs.getString("sfzxys").equals("0")){
                    fzxys=fzxys.add(new BigDecimal(rs.getDouble("jfysy")).divide(wan,4,BigDecimal.ROUND_HALF_UP));
                }
                //je=je+rs.getInt("je");
            }
            //int nys=zxys+fzxys;
            map[i].put("zxys",zxys.toString());//小额采购
            map[i].put("fzxys",fzxys.toString());//非专项预算
            map[i].put("je",je);//金额
            map[i].put("nys",mainTable.getString("yszje"));//年预算
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_jypxjf");
        return  JSON.toJSONString(page);
    }
}
